﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CE.iPhone.PList;
namespace cczEdit.DramaDialog
{
    public partial class EditDramaDialog : Form
    {
        List<Sundry> sundries;
        List<Equipment> weapons;
        List<Equipment> armors;
        List<Equipment> assists;
        List<Hero> heros;
        List<Magic> magics;
        DataGridViewRow currentRow;
        PListDict currentDict;
        string dictType = "InstructType";
        public EditDramaDialog()
        {

            InitializeComponent();
            heros = UCDrama.heros;
            weapons = UCDrama.weapons;
            armors = UCDrama.armors;
            assists = UCDrama.assists;
            sundries = UCDrama.sundries;
            magics = UCDrama.magics;
        }

        
        public DialogResult Show(IWin32Window owner,PListDict dict,TreeNode node)
        {
            currentDict = dict;
            if (node.Parent.Text == "Condition")
                dictType = "TestType";
            else
                dictType = "InstructType";
            PListInteger instructType = (PListInteger)dict[dictType];
            this.Text = Constant.instructTypeDic[(int)instructType.Value];
            dataGridView1.Rows.Clear();
            listBox1.Items.Clear();
            foreach (KeyValuePair<string, IPListElement> kv in dict)
            {
                if (kv.Key == dictType)
                {
                    continue;
                }
                else if (kv.Key.Contains( "SubDrama"))
                {
                    continue;
                }
                
                int index = this.dataGridView1.Rows.Add();
                this.dataGridView1.Rows[index].Cells[0].Value = kv.Key;
                if (kv.Value is PListArray)
                {
                    PListArray values = (PListArray)kv.Value;
                    foreach (PListInteger item in values)
                        this.dataGridView1.Rows[index].Cells[1].Value +=item.Value+",";
                }
                else if (kv.Value is PListBool)
                {
                    PListBool values = (PListBool)kv.Value;
                    this.dataGridView1.Rows[index].Cells[1].Value = values.Value;
                }
                else if (kv.Value is PListInteger)
                {
                    PListInteger values = (PListInteger)kv.Value;
                    this.dataGridView1.Rows[index].Cells[1].Value = values.Value;
                }
                else if (kv.Value is PListString)
                {
                    PListString values = (PListString)kv.Value;
                    this.dataGridView1.Rows[index].Cells[1].Value = values.Value;
                }

                 

            }
           return this.ShowDialog(owner);
        }
        private void buttonAccept_Click(object sender, EventArgs e)
        {
            Form1 mainForm = (Form1)this.Owner;
            PListDict newDict = new PListDict();
            foreach (KeyValuePair<string,IPListElement> kv in currentDict) {
                if (kv.Key == dictType)
                {
                    newDict.Add(kv.Key, kv.Value);
                    continue;
                }
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if ((string)row.Cells[0].Value == (string)kv.Key)
                    {
                        if (kv.Value is PListArray)
                        {
                            PListArray arr = new PListArray();
                            if (row.Cells[1].Value != null)
                            {
                                string[] values = ((string)row.Cells[1].Value).Split(',');
                                foreach (string s in values)
                                {
                                    if (s != "")
                                        arr.Add(new PListInteger(System.Int32.Parse(s)));
                                }
                            }
                            
                            newDict.Add(kv.Key, arr);
                        }
                        else if (kv.Value is PListBool)
                        {
                            bool value = System.Boolean.Parse(row.Cells[1].Value.ToString());
                            newDict.Add(kv.Key,  new PListBool(value));
                        }
                        else if (kv.Value is PListInteger)
                        {
                            long value = System.Int32.Parse(row.Cells[1].Value.ToString());
                            newDict.Add(kv.Key,new PListInteger(value));

                        }
                        else if (kv.Value is PListString)
                        {
                            newDict.Add(kv.Key,  new PListString(row.Cells[1].Value.ToString()));

                        }
                        break;
                    }
                }
            }
            mainForm.ucDrama.editNormalInstruct(newDict);
            this.Hide();
        }

        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
           
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string dataType = currentRow.Cells[0].Value.ToString();
            if (currentRow.Cells[1].Value!=null&&(currentRow.Cells[1].Value.ToString().Contains("True") 
                || currentRow.Cells[1].Value.ToString().Contains("False")))
            {
                currentRow.Cells[1].Value = listBox1.SelectedItem.ToString();
                return;
            }
            if (dataType == "UnitID"||dataType.Contains("UnitID"))
            {
                Hero hero = heros.Find((h)=>{return h.GetName() == listBox1.SelectedItem.ToString();});
                if (hero == null)
                {
                    return;

                }else
                    currentRow.Cells[1].Value = hero.num;
            }
            else if (dataType == "MagicNum")
            {
                Magic magic = magics.Find((h) => { return h.name == listBox1.SelectedItem.ToString(); });
                currentRow.Cells[1].Value = magic.num;
            }
            else if (dataType == "UnitsIDLimited"||dataType == "UnitsIDUnBattle")
            {
                Hero hero = heros.Find((h) => { return h.GetName() == listBox1.SelectedItem.ToString(); });
                
                currentRow.Cells[1].Value +=hero.num+",";
            }
            else if (dataType == "Direction" || dataType == "Weather" 
                || dataType == "WeatherType" || dataType == "AttributeType"
                ||dataType == "SymbolType"||dataType == "ItemType"
                ||dataType=="ObjectType"||dataType == "Owner")
            {
                currentRow.Cells[1].Value = listBox1.SelectedIndex;
            }
            else if (dataType == "TileType")
            {
                foreach (KeyValuePair<int, string> kv in Constant.terrainDic)
                {
                    if (kv.Value == listBox1.SelectedItem.ToString())
                    {
                        currentRow.Cells[1].Value = kv.Key;
                    }
                }
            }
            else if (dataType == "RActionType")
            {
                foreach (KeyValuePair<int, string> kv in Constant.rDramaActionTag)
                {
                    if (kv.Value == listBox1.SelectedItem.ToString())
                    {
                        currentRow.Cells[1].Value = kv.Key;
                    }
                }
            }
            else if (dataType == "MessageBoxType")
            {
                foreach (KeyValuePair<int, string> kv in Constant.messageBoxType)
                {
                    if (kv.Value == listBox1.SelectedItem.ToString())
                    {
                        currentRow.Cells[1].Value = kv.Key;
                    }
                }
            }
            else if (dataType == "SActionType")
            {
                foreach (KeyValuePair<int, string> kv in Constant.sDramaActionTag)
                {
                    if (kv.Value == listBox1.SelectedItem.ToString())
                    {
                        currentRow.Cells[1].Value = kv.Key;
                    }
                }
            }
            else if (dataType == "ArenaActionTag")
            {
                foreach (KeyValuePair<int, string> kv in Constant.arenaActionTag)
                {
                    if (kv.Value == listBox1.SelectedItem.ToString())
                    {
                        currentRow.Cells[1].Value = kv.Key;
                    }
                }
            }
            else if (dataType == "ArenaAttack1ActionTag")
            {
                foreach (KeyValuePair<int, string> kv in Constant.arenaAttack1ActionTag)
                {
                    if (kv.Value == listBox1.SelectedItem.ToString())
                    {
                        currentRow.Cells[1].Value = kv.Key;
                    }
                }
            }
            else if (dataType == "ArenaAttack2ActionTag")
            {
                foreach (KeyValuePair<int, string> kv in Constant.arenaAttack2ActionTag)
                {
                    if (kv.Value == listBox1.SelectedItem.ToString())
                    {
                        currentRow.Cells[1].Value = kv.Key;
                    }
                }
            }
            else if (dataType == "ItemID")
            {
                Sundry sundry=null;
                Equipment equipment=null;
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if ((string)row.Cells[0].Value != "ItemType")
                        continue;
                    int typeCode=Int32.Parse( row.Cells[1].Value.ToString());
                    switch (typeCode)
                    {
                        case 0:
                            sundry = sundries.Find((h) => { return h.name == listBox1.SelectedItem.ToString(); });
                            break;
                        case 1:
                            equipment = weapons.Find((h) => { return h.name == listBox1.SelectedItem.ToString(); });
                            break;
                        case 2:
                            equipment = armors.Find((h) => { return h.name == listBox1.SelectedItem.ToString(); });
                            break;
                        case 3:
                            equipment = assists.Find((h) => { return h.name == listBox1.SelectedItem.ToString(); });
                            break;
                    }
                    break;
                }
                if(sundry!=null)
                    currentRow.Cells[1].Value = sundry.num;
                if (equipment!= null)
                    currentRow.Cells[1].Value = equipment.num;
            }

        }

        private void listBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            List<string> itemSpellList = new List<string>();
            foreach (object o in listBox1.Items)
            {
                itemSpellList.Add(Constant.GetCNCharSpell(o.ToString()));
            }
            for (int i = 0; i != itemSpellList.Count; ++i)
            {
                if (itemSpellList[i] == e.KeyChar.ToString().ToUpper())
                    listBox1.SelectedIndex = i;
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
                return;
            listBox1.Items.Clear();
            if (dataGridView1.Rows.Count <= 0)
                return;
            currentRow = dataGridView1.Rows[e.RowIndex];
            string dataType = currentRow.Cells[0].Value.ToString();
            if (currentRow.Cells[1].Value!=null&&(currentRow.Cells[1].Value.ToString().Contains("True")
                || currentRow.Cells[1].Value.ToString().Contains("False")))
            {
                listBox1.Items.Add("True");
                listBox1.Items.Add("False");
                return;
            }
            if (dataType == "UnitID" || dataType.Contains("UnitID"))
            {
                foreach (Hero hero in heros)
                {
                    listBox1.Items.Add(hero.GetName());
                }
                //-1：无；-2：我军当前人物；-3：任何部队；-4：我军或友军；-5：敌军
            }
            else if (dataType == "MagicNum")
            {
                foreach (Magic magic in magics)
                {
                    listBox1.Items.Add(magic.name);
                }
            }
            else if (dataType == "UnitsIDLimited"||dataType == "UnitsIDUnBattle")
            {
                foreach (Hero hero in heros)
                {
                    listBox1.Items.Add(hero.GetName());
                }

            }
            else if (dataType == "Direction")
            {
                listBox1.Items.Add("东");
                listBox1.Items.Add("西");
                listBox1.Items.Add("南");
                listBox1.Items.Add("北");

            }
            else if (dataType == "RActionType")
            {
                foreach (KeyValuePair<int, string> kv in Constant.rDramaActionTag)
                {
                    listBox1.Items.Add(kv.Value);
                }
            }
            else if (dataType == "Weather")
            {
                listBox1.Items.Add("正常");
                listBox1.Items.Add("晴天");
                listBox1.Items.Add("下雨");
                listBox1.Items.Add("小雪");
                listBox1.Items.Add("大雪");
            }
            else if (dataType == "WeatherType")
            {
                listBox1.Items.Add("晴明");
                listBox1.Items.Add("阴天");
                listBox1.Items.Add("小雨");
                listBox1.Items.Add("大雨");
                listBox1.Items.Add("下雪");
            }
            else if (dataType == "ObjectType")
            {
                listBox1.Items.Add("火");
                listBox1.Items.Add("船");
                listBox1.Items.Add("起火船");
                listBox1.Items.Add("城门");
            }
            else if (dataType == "TileType")
            {
                foreach (KeyValuePair<int, string> kv in Constant.terrainDic)
                {
                    listBox1.Items.Add(kv.Value);
                }
            }

            else if (dataType == "AttributeType")
            {
                foreach (KeyValuePair<int, string> kv in Constant.attributeType)
                {
                    listBox1.Items.Add(kv.Value);
                }
            }
            else if (dataType == "SymbolType")
            {
                foreach (KeyValuePair<int, string> kv in Constant.symbolType)
                {
                    listBox1.Items.Add(kv.Value);
                }
            }
            else if (dataType == "SActionType")
            {
                foreach (KeyValuePair<int, string> kv in Constant.sDramaActionTag)
                {
                    listBox1.Items.Add(kv.Value);
                }
            }
            else if (dataType == "ArenaActionTag")
            {
                foreach (KeyValuePair<int, string> kv in Constant.arenaActionTag)
                {
                    listBox1.Items.Add(kv.Value);
                }
            }
            else if (dataType == "ArenaAttack1ActionTag")
            {
                foreach (KeyValuePair<int, string> kv in Constant.arenaAttack1ActionTag)
                {
                    listBox1.Items.Add(kv.Value);
                }
            }
            else if (dataType == "ArenaAttack2ActionTag")
            {
                foreach (KeyValuePair<int, string> kv in Constant.arenaAttack2ActionTag)
                {
                    listBox1.Items.Add(kv.Value);
                }
            }
            else if (dataType == "ItemType")
            {
                listBox1.Items.Add("消耗品");
                listBox1.Items.Add("武器");
                listBox1.Items.Add("防具");
                listBox1.Items.Add("辅助装备");
            }
            else if (dataType == "MessageBoxType")
            {
                foreach (KeyValuePair<int, string> kv in Constant.messageBoxType)
                {
                    listBox1.Items.Add(kv.Value);
                }
            }
            else if (dataType == "ItemID")
            {
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if ((String)row.Cells[0].Value != "ItemType")
                        continue;
                    switch (System.Int32.Parse( row.Cells[1].Value.ToString()))
                    {
                        case 0:
                            foreach (Sundry sundry in sundries)
                                listBox1.Items.Add(sundry.name);
                            break;
                        case 1:
                            foreach (Equipment sundry in weapons)
                                listBox1.Items.Add(sundry.name);
                            break;
                        case 2:
                            foreach (Sundry sundry in armors)
                                listBox1.Items.Add(sundry.name);
                            break;
                        case 3:
                            foreach (Sundry sundry in assists)
                                listBox1.Items.Add(sundry.name);
                            break;

                    }
                    break;

                }
            }
            else if (dataType == "Owner")
            {
                foreach (KeyValuePair<int, string> kv in Constant.ownTypeDic)
                {
                    listBox1.Items.Add(kv.Value);
                }
            }
        }
    }
}
